package com.lihua.phonecode.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lihua.phonecode.service.PhoneCodeService;
import com.lihua.phonecode.utils.ucpaas.restDemo.client.JsonReqClient;
import lombok.Data;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 生产手机验证码，并发送给手机验证。注意：使用properties的方式注入，一定要有set方法
 * @author 15594
 */
@Service
@PropertySource(value = {"classpath:config.properties"})
@ConfigurationProperties(prefix = "yzx")
@Data
public class PhoneCodeServiceImpl implements PhoneCodeService {


    /**
     * 短信接口需要的参数
     */
    private String sid;
    private String token;
    private String appid;
    private String regTemplateid;
    private String faultTemplateid;

    /**
     * RedisTemplate
     * */
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String getRandomCode() {
        Random random = new Random();
        int code = 1000+random.nextInt(8999);
        return Integer.toString(code);
    }

    @Override
    public boolean sendCode(String code, String phone) {
        System.out.println(sid);
        JsonReqClient jsonReqClient = new JsonReqClient();
        String result = jsonReqClient.sendSms(sid, token, appid, regTemplateid, code, phone, null);
        System.out.println("响应:"+result);
        JSONObject jsonObject = JSON.parseObject(result);
        String code1 = jsonObject.getString("code");
        if ("000000".equals(code1)){
            redisTemplate.opsForValue().set(phone,code,60*3, TimeUnit.SECONDS);
            return true;
        }else {
            return false;
        }

    }

    @Override
    public int isPhoneCode(String code, String phone) {
        String  Rcode= (String)redisTemplate.opsForValue().get(phone);
        System.out.println(Rcode);
        if (Rcode!=null){
            if (Rcode.equals(code)){
                redisTemplate.delete(phone);
                redisTemplate.delete("lock-"+ phone);
                return 1;
            }else {
                Long increment = redisTemplate.opsForValue().increment("lock-" + phone);
                return -1;
            }

        }else {
            return 0;
        }
    }

    @Override
    public String lockPhone(String phone) {
        if (redisTemplate.opsForValue().get("lock-" + phone)!=null){
            int i = (int)redisTemplate.opsForValue().get("lock-" + phone);
            System.out.println(i);
            if (i<5){
                return "输入错误"+(5-i)+"后账号将锁定24小时！";
            }else {
                return "lock";
            }
        }else {
            return "unlock";
        }

    }

}
